package bid.yanhj.yanhaijiang.service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

/**
 * 四大组件之一
 * 常驻后台
 * Service主要运行与主线程
 * 如果需要密集型（MP3播放）或阻塞（网络）操作，需要开启子线程
 *
 * 启动方式
 * context.startService()  持续
 * context.bindService()  交互
 * 有不同的生命周期
 *
 * startService 只有stop 才会走销毁
 *
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button sartServiceBtn, stopServiceBtn;
    private Button bindServiceBtn, unbindServiceBtn;
    private Button sendServiceBtn, getServiceBtn;

    private ServiceConnection connection;

    private MainService.MyBinder myBinder;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sartServiceBtn = findViewById(R.id.start_service_btn);
        stopServiceBtn = findViewById(R.id.stop_service_btn);

        sartServiceBtn.setOnClickListener(this);
        stopServiceBtn.setOnClickListener(this);

        //
        bindServiceBtn = findViewById(R.id.bind_service_btn);
        unbindServiceBtn = findViewById(R.id.unbind_service_btn);

        bindServiceBtn.setOnClickListener(this);
        unbindServiceBtn.setOnClickListener(this);

        //
        sendServiceBtn = findViewById(R.id.send_data_to_service_btn);
        getServiceBtn = findViewById(R.id.get_data_from_service_btn);

        sendServiceBtn.setOnClickListener(this);
        getServiceBtn.setOnClickListener(this);

        connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                myBinder = (MainService.MyBinder) service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                myBinder = null;
            }
        };
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, MainService.class);
        switch (v.getId()) {
            case R.id.start_service_btn:
                startService(intent);
                break;
            case R.id.stop_service_btn:
                stopService(intent);
                break;
            case R.id.bind_service_btn:
                // 绑定自动创建service
                bindService(intent, connection, BIND_AUTO_CREATE);
                break;
            case R.id.unbind_service_btn:
                unbindService(connection);
                break;
            case R.id.send_data_to_service_btn:
                if (myBinder != null) {
                    myBinder.setData("这是Ary发送数据");
                }
                break;
            case R.id.get_data_from_service_btn:
                if (myBinder != null) {
                    String data = myBinder.getData();
                    Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
